home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / plane / _point.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  2.7 KB  |  132 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _point.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #include <LEDA/segment.h>
  17. #include <math.h>
  18. #include <ctype.h>
  19.  
  20. static const double eps = 1e-10;
  21.  
  22.  
  23. //------------------------------------------------------------------------------
  24. // points 
  25. //------------------------------------------------------------------------------
  26.  
  27.  
  28. point_rep::point_rep()  { count=1; x = y = 0.0; }
  29.  
  30. point_rep::point_rep(double a, double b) 
  31. { x = a; 
  32.   y = b; 
  33.   count = 1; 
  34. }
  35.  
  36.  
  37.  
  38. point::point()                  { PTR = new point_rep; }
  39. point::point(double x, double y){ PTR = new point_rep(x,y); }
  40. point::point(vector v)          { PTR = new point_rep(v[0], v[1]); }
  41.  
  42. double point::angle(const point& q, const point& r) const
  43. {
  44.   double cosfi,fi,norm;
  45.   
  46.   double dx  = q.ptr()->x - ptr()->x; 
  47.   double dy  = q.ptr()->y - ptr()->y; 
  48.  
  49.   double dxs = r.ptr()->x - q.ptr()->x; 
  50.   double dys = r.ptr()->y - q.ptr()->y; 
  51.   
  52.   cosfi=dx*dxs+dy*dys;
  53.   
  54.   norm=(dx*dx+dy*dy)*(dxs*dxs+dys*dys);
  55.  
  56.   cosfi /= sqrt( norm );
  57.  
  58.   if (cosfi >=  1.0 ) return 0;
  59.   if (cosfi <= -1.0 ) return M_PI;
  60.   
  61.   fi=acos(cosfi);
  62.  
  63.   if (dx*dys-dy*dxs>0) return fi;
  64.  
  65.   return -fi;
  66. }
  67.   
  68.  
  69. point point::rotate(const point& origin, double alpha) const
  70. { if (origin == *this) return *this;
  71.   segment s(origin,*this);
  72.   return s.rotate(alpha).end();
  73. }
  74.  
  75. point point::rotate(double alpha) const
  76. { return rotate(point(0,0),alpha); }
  77.  
  78. point point::translate(double alpha, double d) const
  79. { double dx = cos(alpha) * d;
  80.   double dy = sin(alpha) * d;
  81.   return point(ptr()->x+dx,ptr()->y+dy);
  82.  }
  83.  
  84. point point::translate(const vector& v) const
  85. { return point(ptr()->x+v[0],ptr()->y+v[1]);
  86.  }
  87.  
  88. double point::distance(const point& p)  const
  89. { return hypot(p.ptr()->x - ptr()->x, p.ptr()->y - ptr()->y); } 
  90.  
  91. double point::distance() const
  92. { return distance(point(0,0)); }
  93.  
  94.  
  95. int point::operator==(const point& p) const 
  96. { return (fabs(ptr()->x - p.ptr()->x) < eps && fabs(ptr()->y - p.ptr()->y) < eps); }
  97.  
  98.  
  99. ostream& operator<<(ostream& out, const point& p)
  100. { out << "(" << p.xcoord() << "," << p.ycoord() << ")";
  101.   return out;
  102.  } 
  103.  
  104. istream& operator>>(istream& in, point& p) 
  105. { // syntax: {(} x {,} y {)}
  106.  
  107.   double x,y; 
  108.   char c;
  109.  
  110.   do in.get(c); while (in && isspace(c));
  111.  
  112.   if (!in) return in;
  113.  
  114.   if (c != '(') in.putback(c);
  115.  
  116.   in >> x;
  117.  
  118.   do in.get(c); while (isspace(c));
  119.   if (c != ',') in.putback(c);
  120.  
  121.   in >> y; 
  122.  
  123.   do in.get(c); while (c == ' ');
  124.   if (c != ')') in.putback(c);
  125.  
  126.   p = point(x,y); 
  127.   return in; 
  128.  
  129.  } 
  130.  
  131.